package com.polytech.rsa;

import java.math.BigInteger;
import java.util.Random;

public class RSAKeysGenerator {
	
	BigInteger q;
	BigInteger p;
	private BigInteger phin;
	private BigInteger n;
	private final int KEY_LENGHT=32;
	private final int PQ_LENGHT=16;
	private BigInteger e,d;
	
	public RSAKeysGenerator() {
			
	}
	public BigInteger generatePublicKey(){
		p=generatePrimierNumber(PQ_LENGHT);
		while(true){
		 q=generatePrimierNumber(PQ_LENGHT);
		 if(q!=p)
			 break;
		 }
		n=p.multiply(q);
		phin=p.subtract(new BigInteger("1")).multiply(q.subtract(new BigInteger("1")));
		while(true){
		  e=new BigInteger(KEY_LENGHT, new Random());
			if(e.gcd(phin).equals(new BigInteger("1"))){
				break;
			}
		}
		return e;
	}
	
	public BigInteger generatePrivateKey(){
		d=e.modInverse(phin);
		return d;
	}
	private BigInteger generatePrimierNumber(int lenght) {
		 return BigInteger.probablePrime(lenght, new Random());
	}
	public static void main(String[] args) {
		new RSAKeysGenerator();
		
	}
	public BigInteger getN() {
		// TODO Auto-generated method stub
		return n;
	}
	
}
